--
-- Author: shunguo.chen
-- Date: 2018-11-19 11:42:03
--
-- TeamTargetFirstRender 组队查找器一级菜单
--

local ClassRef = app.mvc.createRenderClass(...)

local COLOR_SELECTED = cc.c3b( 255, 225, 132 )
local COLOR_UNSELECTED = cc.c3b( 97, 97, 97 )

--data = {id=, tp=, name=, subTargets={}, curSubTargets={}}
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.TeamTargetNode_1))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self:setContentSize(self._ui.Panel:getContentSize())

    self._ui.Panel:setTouchEnabled(true)
    self._ui.selected_btn:setTouchEnabled(false)
    display.uiAddClick(self._ui.Panel, function()
        executeFunc(self._onClick, self, self:isSelected(), self._data)
    end)
    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        self._ui.name:setString(self._data.name)

        local hasSub = #self._data.curSubTargets > 0
        local selected = self:isSelected()
        self._ui.img_1:setVisible(hasSub and selected)
        self._ui.img_2:setVisible(hasSub and not selected)

        self:refreshAutoRecruit()
        self:refreshAutoApply()
        self:refreshInCrossMatch()
    end
end

function ClassRef:setSelected(b)
    self._ui.selected_btn:setBright(not b)

    local hasSub = #self._data.curSubTargets > 0
    self._ui.img_1:setVisible(hasSub and b)
    self._ui.img_2:setVisible(hasSub and not b)

    local color = b and COLOR_SELECTED or COLOR_UNSELECTED
    self._ui.name:setTextColor(color)
end

function ClassRef:isSelected()
    return not self._ui.selected_btn:isBright()
end

-- 自动招募标记，已经有队伍可能出现，只有一个
function ClassRef:refreshAutoRecruit()
    local isAuto = false
    if #self._data.curSubTargets > 0 then
        for i,target in ipairs(self._data.curSubTargets) do
            isAuto = isAuto or target:isAutoRecruit(true)
            if isAuto then
                break
            end
        end
    end
    self._ui.icon_1:setVisible(isAuto)
end

-- 自动申请标记，没有队伍时可能出现，可以有多个
function ClassRef:refreshAutoApply()
    local isAuto = false
    if #self._data.curSubTargets > 0 then
        for i,target in ipairs(self._data.curSubTargets) do
            isAuto = isAuto or target:isAutoApply(true)
            if isAuto then
                break
            end
        end
    end
    self._ui.icon_2:setVisible(isAuto)
end

-- 跨服匹配标记
function ClassRef:refreshInCrossMatch()
    local isAuto = false
    if #self._data.curSubTargets > 0 then
        for i,target in ipairs(self._data.curSubTargets) do
            isAuto = isAuto or target:isInCrossMatch(true)
            if isAuto then
                break
            end
        end
    end
    self._ui.icon_3:setVisible(isAuto)
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
